<?php
// 简易语音测试HTTP服务（使用 PHP 内置服务器作为路由脚本）
// 启动命令：php -S 127.0.0.1:8787 tools/device_tcp/voice_test_server.php

require_once __DIR__ . '/simple_server.php'; // 复用 playVoice()

function respond_json($data, int $code = 200) {
    http_response_code($code);
    header('Content-Type: application/json; charset=utf-8');
    echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
}

$path = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH) ?: '/';
if ($path === '/' || $path === '/index') {
    // 简单页面，便于点击测试
    header('Content-Type: text/html; charset=utf-8');
    echo '<!doctype html><meta charset="utf-8"><title>语音测试</title>';
    echo '<h3>语音测试</h3><ul>';
    echo '<li><a href="/play?type=success" target="_blank">播放：签到成功</a></li>';
    echo '<li><a href="/play?type=end" target="_blank">播放：结束签到</a></li>';
    echo '<li><a href="/play?type=duplicate" target="_blank">播放：请勿重复刷脸</a></li>';
    echo '</ul><p>也可直接调用：/play?type=success|end|duplicate</p>';
    exit;
}

if ($path === '/play') {
    $type = strtolower(strval($_GET['type'] ?? 'success'));
    try {
        playVoice($type);
        respond_json(['ok' => true, 'type' => $type, 'time' => date('Y-m-d H:i:s')]);
    } catch (Throwable $e) {
        respond_json(['ok' => false, 'error' => $e->getMessage()], 500);
    }
    exit;
}

// 其他路径：404
respond_json(['ok' => false, 'error' => 'Not Found'], 404);

